package miku.firstgame.model.proxy
{
	import flash.geom.Point;

	import miku.firstgame.constrants.AppConstrants;
	import miku.firstgame.constrants.ClientParam;
	import miku.firstgame.constrants.ProtocalConstants;
	import miku.firstgame.entity.Bomb;
	import miku.firstgame.model.Me;
	import miku.firstgame.model.Room;
	import miku.firstgame.model.User;
	import miku.firstgame.model.gold.GlodUserType;
	import miku.firstgame.net.IConnection;
	import miku.firstgame.net.pack.ChangeCurrentTurnResponsePack;
	import miku.firstgame.net.pack.ExitRoomResponsePack;
	import miku.firstgame.net.pack.GoldCompleteResponsePack;
	import miku.firstgame.net.pack.GoldGameStartResponsePack;
	import miku.firstgame.net.pack.LoginResponsePack;
	import miku.firstgame.net.pack.RemoveRoadCardResponsePack;
	import miku.firstgame.net.pack.SeeMapResponsePack;
	import miku.firstgame.net.pack.SendUserACardResponsePack;
	import miku.firstgame.net.pack.SetRoadCardResponsePack;
	import miku.firstgame.net.pack.SetToolToUserResponsePack;
	import miku.firstgame.net.pack.UserExitRoomResponsePack;
	import miku.firstgame.net.proxy.Connection;

	import org.puremvc.as3.patterns.proxy.Proxy;

	public class MeProxy extends Proxy
	{
		public static const NAME:String = "MeProxy";

		private var me:Me;

		public function MeProxy(me:Me)
		{
			super(NAME, me);
			this.me = me;
		}

		override public function onRegister():void
		{
			super.onRegister();
			//Connection.getInstance().registerProtocolInterest(handleLogin, ["login"]);
			Connection.getInstance().registerProtocolInterest(handleExitRoom, [ProtocalConstants.EXIT_ROOM]);
			Connection.getInstance().registerProtocolInterest(handleUserExitRoom, [ProtocalConstants.USER_EXIT_ROOM]);
			Connection.getInstance().registerProtocolInterest(handleBLogin, [ProtocalConstants.LOGIN]);
			Connection.getInstance().registerProtocolInterest(handleGoldGameStart, [ProtocalConstants.GOLD_GAME_START]);
			Connection.getInstance().registerProtocolInterest(handleUserGetACard, [ProtocalConstants.SEND_USER_A_CARD]);
			Connection.getInstance().registerProtocolInterest(handleSetRoadCard, [ProtocalConstants.SET_ROAD_CARD]);
			Connection.getInstance().registerProtocolInterest(handleSetToolToUser, [ProtocalConstants.SET_TOOL_TO_USER]);
			Connection.getInstance().registerProtocolInterest(handleChangeCurrentTurn, [ProtocalConstants.CHANGE_CURRENT_TURN]);
			Connection.getInstance().registerProtocolInterest(handleRemoveRoadCard, [ProtocalConstants.REMOVE_ROAD_CARD]);
			Connection.getInstance().registerProtocolInterest(handleSeeMap, [ProtocalConstants.SEE_MAP]);
			Connection.getInstance().registerProtocolInterest(handleGoldComplete, [ProtocalConstants.GOLD_COMPLETE]);

		}

		override public function onRemove():void
		{
			super.onRemove();
			//Connection.getInstance().unregisterHandler(handleLogin);
			Connection.getInstance().unregisterHandler(handleExitRoom);
			Connection.getInstance().unregisterHandler(handleUserExitRoom);

			Connection.getInstance().unregisterHandler(handleBLogin);
			Connection.getInstance().unregisterHandler(handleGoldGameStart);

			Connection.getInstance().unregisterHandler(handleUserGetACard);
			Connection.getInstance().unregisterHandler(handleSetRoadCard);
			Connection.getInstance().unregisterHandler(handleSetToolToUser);
			Connection.getInstance().unregisterHandler(handleChangeCurrentTurn);
			Connection.getInstance().unregisterHandler(handleRemoveRoadCard);
			Connection.getInstance().unregisterHandler(handleSeeMap);
			Connection.getInstance().unregisterHandler(handleGoldComplete);
		}

		private function handleGoldComplete(pack:GoldCompleteResponsePack, connection:IConnection):void
		{
			if (pack.result)
			{
				sendNotification(AppConstrants.GOLD_COMPELETE, pack);
			}
			else
			{
				sendNotification(AppConstrants.DUMP_SEE, pack.direct);
			}
		}

		private function handleSeeMap(pack:SeeMapResponsePack, connection:IConnection):void
		{
			if (pack.isGold)
			{
				sendNotification(AppConstrants.GOLD_SEE, pack.direct);
			}
			else
			{
				sendNotification(AppConstrants.DUMP_SEE, pack.direct);
			}
		}

		private function handleRemoveRoadCard(pack:RemoveRoadCardResponsePack, connection:IConnection):void
		{
			sendNotification(AppConstrants.REMOVE_ROAD_CARD, new Point(pack.x, pack.y));
		}

		private function handleChangeCurrentTurn(pack:ChangeCurrentTurnResponsePack, connection:IConnection):void
		{
			me.currentTurn = pack.turn;
			sendNotification(AppConstrants.CHANGE_CURRENT_TURN, pack.turn);
		}

		private function handleSetToolToUser(pack:SetToolToUserResponsePack, connection:IConnection):void
		{
			sendNotification(AppConstrants.SET_TOOL_TO_USER, pack);
		}

		private function handleSetRoadCard(pack:SetRoadCardResponsePack, connection:IConnection):void
		{
//TODO  
			if (pack.hostUserId != me.id)
			{
				sendNotification(AppConstrants.SET_A_ROAD_CARD, pack);
			}
		}

		private function handleUserGetACard(pack:SendUserACardResponsePack, connection:IConnection):void
		{
			sendNotification(AppConstrants.SEND_USER_A_CARD, pack.cardType);
		}

		//TODO
		private function handleGoldGameStart(pack:GoldGameStartResponsePack, connection:IConnection):void
		{
			me.currentTurn = pack.turn;
			me.isGood = (pack.heroType == GlodUserType.GOOD);
			me.cards = pack.cards;
			me.orders = pack.orders;

			sendNotification(AppConstrants.GOLD_GAME_START);
		}

		private function handleUserExitRoom(pack:UserExitRoomResponsePack, connection:IConnection):void
		{
			var user:User = me.removeContactUser(pack.userId);
			if (user)
			{
				//用户退出房间
				sendNotification(AppConstrants.USER_EXIT_GAME_ROOM, user); // TODO 登陆房间没考虑
			}
		}

		private function handleExitRoom(pack:ExitRoomResponsePack, connection:IConnection):void
		{
			var result:Boolean = (pack.result == 0);
			if (result)
			{
				me.clearContactUsers();
				var lobbyProxy:LobbyProxy = facade.retrieveProxy(LobbyProxy.NAME) as LobbyProxy;
				lobbyProxy.setUsers(pack.users);
				var roomProxy:RoomProxy = facade.retrieveProxy(RoomProxy.NAME) as RoomProxy;
				roomProxy.setRooms(pack.rooms);
				me.room = null;
				sendNotification(AppConstrants.EXIT_ROOM_SUCCESS);
			}
			else
			{
				sendNotification(AppConstrants.EXIT_ROOM_FAIL);
			}
		}

		//<login r="0" /> 0表示成功，1表示账号名重复，2表示失败
		private function handleLogin(xml:XML, connection:IConnection):void
		{
			var result:Boolean = (xml.@r.toString() == "0");
			if (result)
			{
				ClientParam.SERVER_MARJOR_VERSION = parseInt(xml.@f.toString());
				ClientParam.SERVER_MINOR_VERSION = parseInt(xml.@s.toString());

				var name:String = xml.@n;
				var id:String = xml.@id;

				me.name = name;
				me.id = parseInt(id);

				lanchRooms(xml.rs[0]);
				lanchUsers(xml.us[0]);

				sendNotification(AppConstrants.USER_LOGIN_SUCCUSS);

			}
			else
			{
				sendNotification(AppConstrants.USER_LOGIN_FAIL);
			}
		}

		private function handleBLogin(pack:LoginResponsePack, connection:IConnection):void
		{
			if (pack)
			{
				if (pack.result == 0)
				{
					ClientParam.SERVER_MARJOR_VERSION = pack.marjorVersion;
					ClientParam.SERVER_MINOR_VERSION = pack.minorVersion;

					me.name = pack.name;
					me.id = pack.id;
					var lobbyProxy:LobbyProxy = facade.retrieveProxy(LobbyProxy.NAME) as LobbyProxy;
					lobbyProxy.setUsers(pack.users);
					var roomProxy:RoomProxy = facade.retrieveProxy(RoomProxy.NAME) as RoomProxy;
					roomProxy.setRooms(pack.rooms);
					sendNotification(AppConstrants.USER_LOGIN_SUCCUSS);
				}
				else
				{
					sendNotification(AppConstrants.USER_LOGIN_FAIL);
				}
			}
		}

		private function lanchUsers(xml:XML):void
		{
			var userXmls:XMLList = xml.u;
			if (userXmls.length() > 0)
			{
				var lobbyProxy:LobbyProxy = facade.retrieveProxy(LobbyProxy.NAME) as LobbyProxy;
				lobbyProxy.clearAllUsers();
				for each (var userXml:XML in userXmls)
				{
					var id:String = userXml.@id;
					var name:String = userXml.@n;
					var status:int = int(userXml.@s.toString());
					var user:User = new User(parseInt(id));
					user.name = name;
					user.status = status;
					lobbyProxy.addUser(user);
				}
			}
		}

		private function lanchRooms(xml:XML):void
		{
			var romXmls:XMLList = xml.r;

			if (romXmls.length() > 0)
			{
				var roomProxy:RoomProxy = facade.retrieveProxy(RoomProxy.NAME) as RoomProxy;
				roomProxy.clearAllRooms();
				for each (var roomXml:XML in romXmls)
				{
					var id:String = roomXml.@id;
					var name:String = roomXml.@n;

					var room:Room = new Room();
					room.roomId = parseInt(id);
					room.roomName = name;
					roomProxy.addRoom(room);
				}
			}
		}
	}
}
